package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ArticleController {
    private  static  String articlesDir="./articles/";
    static {
        File file=new File(articlesDir);
        if(!file.exists()){
            file.mkdirs();
        }
    }
    public void writeArticle(HttpRequest request, HttpResponse response){
        System.out.println("发表文章开始了");
        String title=request.getParameter("title");
        String author=request.getParameter("author");
        String Articles=request.getParameter("Articles");
        if(title==null||author==null||Articles==null){
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            System.out.println("文章发表失败");
            return;
        }
        Article articles=new Article(title,author,Articles);
        try (
                ObjectOutputStream op=new ObjectOutputStream(new FileOutputStream(articlesDir+title+".obj"))
                ){
            op.writeObject(articles);
            response.setEntity(new File("./webapps/myweb/article_success.html"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        System.out.println("发表文章结束了");

    }
    public void showAllArticle(HttpRequest request, HttpResponse response){
        File file=new File(articlesDir);
        List<Article> list=new ArrayList<>();
        if(file.isDirectory()&&file.exists()){
            File[] files=file.listFiles(f->f.getName().endsWith(".obj"));
            for (int i=0;i<files.length;i++){
            try (
                    ObjectInputStream obs=new ObjectInputStream(new FileInputStream(files[i]));
                    ) {
                list.add((Article)obs.readObject());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            }

            }
        list.forEach(System.out::println);
        PrintWriter pw=response.getWriter();
        pw.println("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>文章列表</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<center>\n" +
                "    <h1>用户列表</h1>\n" +
                "    <table border=\"1\">\n" +
                "        <tr>\n" +
                "            <td> 作者</td>\n" +
                "            <td> 标题</td>\n" +
                "        </tr>\n");
        for(int i=0;i<list.size();i++){
            pw.println( "        <tr>\n" +
                    "            <td> "+list.get(i).getAuthor()+"</td>\n" +
                    "            <td> "+list.get(i).getTitle()+"</td>\n" +
                    "        </tr>\n");
        }
        pw.println("</table>\n" +
                "</center>\n" +
                "</body>\n" +
                "</html>");
        response.setContentType("text/html");
        System.out.println("动态页面生成完毕");

        }

    }

